<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <title>9-24获取用户信息测试.html</title>
    <script>
        $(function () {
            let userId = JSON.parse(localStorage.getItem('currentUser'))._id //从登录后保存的本地存储数据中获取用户id
            $.ajax({
                url: "http://localhost:3000/api/getUserInfoById",
                type: 'get',
                data: { userId },
                success: function (res) {
                    if (res.message == "ok") {
                        $('.userName ').html('姓名:' + res.user.name)
                        $('.pichead ').attr('src', `http:/ /localhost:3000/${res.user.headpic}`)
                        $('.sex').html(`性别: ${res.user.sex ? '男' : '女'}`)
                        $('.mobile').html(`电话: ${res.user.mobile}`)
                        $('.birth').html(`生日:${res.user.birth}`)
                        $('address').html(`地址:${res.user.address}`)
                    } else {
                        alert('个人信息获取失败!')
                    }
                }
            })
        })

        function changePwd() {
            //从登录后保存的本地存储数据中获取用户id
            let userId = JSON.parse(localStorage.getItem('loginUser'))._id
            //获取老密码
            let userOldPwd = $('.oldPwd').val()
            //获取新密码
            let userNewPwd = $('.newPwd').val()
            $.ajax({
                url: 'http://localhost:3000/api/changePwd',
                type: 'post',
                data: { userId, userOldPud, userNewPwd },
                success: function (res) {
                    if (res.message == 'ok') {
                        alert('修改密码成功!')
                    } else {
                        alert(res.message)
                    }
                }
            })
        }


        function selectFile() {
            //打开文件选择框
            $('#headPic').click()
        }

        function uploadHeadPic(file) {
            let userId = JSON.parse(localStorage.getItem('loginUser'))._id //从登录后保存的本地存储数据中获取用户id
            let fd = new FormData()
            //保存上传文件
            fd.append('file', file.files[0])
            //保存上传的用户id
            fd.append('userId', userId)
            $.ajax({
                url: "http://localhost:3000/api/uploadHeadPic",
                type: 'post',
                data: fd,
                processData: false,
                contentType: false,
                success: function (res) {
                    if (res.message == 'ok') {
                        let headpic = `http://localhost:3000/upload/${res.data}`
                        $('.pichead').attr("src", headpic)
                    } else {
                        alert(res.message)
                    }
                }
            })
        }

    </script>
</head>

<body>
    <div class="info">
        <h2>基本信息</h2>
        <img src="" alt="" class="pichead">
        <h3 class="userName">姓名:</h3>
        <p class="sex">性别:</p>
        <p class="phone">手机号:</p>
        <p class="birth">生日:</p>
        <p class="address">地址:</p>
    </div>
    <div class="changePwd">
        原密码:<input type="password" name="oldPwd" id="" class="oldPwd">
        新密码:<input type="password" name="newPwd" id="" class="newPwd">
        <input type="button" value="修改密码" onclick="changePwd()">
    </div>

</body>

</html>